package com.example.source_share.controller.user.account;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.source_share.mapper.UserMapper;
import com.example.source_share.pojo.User;
import com.example.source_share.service.user.account.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class LoginController {
    //注入接口
    @Autowired
    private LoginService loginService;

    @Autowired
    UserMapper userMapper;
    @PostMapping("/user/account/token")
    public Map<String ,String > getToken(@RequestParam Map<String, String > map){//要将post请求的参数放到map里

        String username = map.get("username");
        String encode_pass = map.get("password");
        StringBuilder password= new StringBuilder();
        //System.out.println(encode_pass);
        //简单解密
        String str1 = "L73hz/sHgnKmQCD8YFfGj1toaR0S4JcTpi6rv5yw+BVbWPeNu2IlUOxEdqkM9XZ+A-*&^%$#@!";
        String str2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-/=!@#$%^&*";
        for (int i = 0; i < encode_pass.length(); i++) {
            int k = str1.indexOf(encode_pass.charAt(i));
            password.append(str2.charAt(k));
        }
        //System.out.println(password);
        return loginService.getToken(username, password.toString());
    }
    /* 已改成更安全的方式
    @GetMapping("/user/check/{username}/{password}")
    public String loginIn( //用户登入
                           @PathVariable String username,
                           @PathVariable String password){
        //查看用户是否存在
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",username);
        User exist=userMapper.selectOne(queryWrapper);
        if(exist==null){
            return "用户不存在";
        }
        PasswordEncoder passwordEncoder=new BCryptPasswordEncoder();
        boolean right=passwordEncoder.matches(password,exist.getPassword());
        if(right)
        return "Success!";
        else return "用户名或密码错误";
    }

     */
}
